/**
**  采集 获取163 网易新闻
*/
const https = require('https');

const cheerio = require('cheerio');
const Content = require('../../models/Content');
const Category = require('../../models/Category');

// const Tag = require('../../models/Tag.js');
// var _ = require('lodash');

/*
*  采集接口
* */
// let urlList = [
//     'https://3g.163.com/touch/reconstruct/article/list/BD21K0DLwangning/0-10.html', // 轻松一刻
//     'https://3g.163.com/touch/reconstruct/article/list/CQ9UDVKOwangning/0-10.html', // 胖编怪聊
//     'https://3g.163.com/touch/reconstruct/article/list/CQ9UJIJNwangning/0-10.html', // 曲一刀
// ];

const categoryName = '博海拾贝';

const mapUrlList = [
  {
    url: 'https://www.bohaishibei.com/post/category/main/',
    name: '博海拾贝',
  },
//   {
//     url: 'https://3g.163.com/touch/reconstruct/article/list/CQ9UJIJNwangning/0-10.html',
//     name: '曲一刀',
//     value: 'CQ9UJIJNwangning',
//     suffix: 'CQ9UJIJNwangning',
//   },
];

/**
 * 开始采集
 */
const startGet_163_Text = async () => {
  for (let i = 0; i < mapUrlList.length; i++) {
    const item = mapUrlList[0];
    const { url } = item;
    let _categoryId = '';
    const _categoryName = categoryName;
    const findAtCategory = await Category.findOne({
      name: _categoryName,
    });
    // console.log('findAtCategory', findAtCategory);
    if (findAtCategory) {
      _categoryId = findAtCategory._id;
      if (_categoryId) {
        https.get(url, (res) => {
          // 分段返回的 自己拼接
          let html = '';
          // 有数据产生的时候 拼接
          res.on('data', (chunk) => {
            html += chunk;
          });
          // 拼接完成
          res.on('end', () => {
            //   console.log(html);
            if (html) {
              booksQuery(html, _categoryId, item.suffix);
            }
          });
        });
      }
    } else {
    //   console.log(_categoryName, '_categoryName');
      await new Category({
        name: _categoryName,
      }).save();
    }
  }
};

function booksQuery(body, categoryId, contentKey) {
//   console.log(body, 'body');
//   console.log(categoryId, 'categoryId');

  const $ = cheerio.load(body, { decodeEntities: false });
  const $articleList = $('#recent-content').find('.post');
  const articleUrlList = [];

  $articleList.each((idx, element) => {
    // console.log(element, 'element');
    const url = $(element).find('.thumbnail-link').attr('href');
    // console.log(url, 'url');
    articleUrlList.push(url);
  });
  //   console.log(articleUrlList, 'articleUrlList');

  getEveryNews(articleUrlList, categoryId);
}

function getEveryNews(articleUrlList = [], categoryId) {
  // 获取列表页的 文章列表，然后每篇独自采集
  if (articleUrlList.length === 0) return;
  const recentArticleUrl = articleUrlList[0];
  https.get(recentArticleUrl, (res) => {
    // 分段返回的 自己拼接
    let html = '';
    // 有数据产生的时候 拼接
    res.on('data', (chunk) => {
      html += chunk;
    });
    // 拼接完成
    res.on('end', async () => {
      const content = {};
      if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        const $article = $('#primary');
        content.title = $article.find('h1.entry-title').text().trim();
        content.thumb = $article.find('.entry-content').find('img').eq(0).attr('src');
        content.description = $article.find('.entry-content').text().replace(/\s+/g, '');

        // 关键词是否有bug待定
        // content.keywords = getKeyWord(content.title, 2).join(',');

        if (content.description.length > 72) {
          content.description = content.description.substring(0, 72);
        }
        content.category = categoryId;
        content.content = $article.find('.entry-content').html();
        content.originalLink = recentArticleUrl;

        content.content = content.content.replace(/src=/g, 'data-src=');
        // console.log(content, 'content');
        const condition = {
          $or: [{ title: content.title }, { docid: content.docid }],
        };
        try {
          const isHasContent = await Content.findOne(condition);
          if (!isHasContent) {
            new Content(content).save();
          }
        } catch (error) {

        }
      }
    });
  });
}

exports.startGet_bohaishibei_Text = startGet_163_Text;
